#include "blur.h"
#include <dirent.h>
#include <iostream>
#include <locale.h>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>

using namespace std;
using namespace cv;
/********************************************************************************
 *函数描述： DefRto 计算并返回一幅图像的清晰度
 *函数参数： frame 彩色帧图
 *函数返回值：double
 *清晰度表示值，针对该视频，当清晰度小于10为模糊，大于14为清楚
 *********************************************************************************/
bool DefRto(cv::Mat frame) {
  Mat gray;
  cvtColor(frame, gray, CV_BGR2GRAY);
  double temp = 0;
  double DR = 0;
  int i, j; //循环变量
  int height = gray.rows;
  int width = gray.cols;
  int step = gray.step / sizeof(uchar);
  uchar *data = (uchar *)gray.data;
  double num = width * height;
  for (i = 0; i < height; i++) {
    for (j = 0; j < width; j++) {
      temp += sqrt(
          (pow((double)(data[(i + 1) * step + j] - data[i * step + j]), 2) +
           pow((double)(data[i * step + j + 1] - data[i * step + j]), 2)));
      temp += abs(data[(i + 1) * step + j] - data[i * step + j]) +
              abs(data[i * step + j + 1] - data[i * step + j]);
    }
  }
  DR = temp / num;
  if (DR <= 10) {
    cout << "画面模糊！！！清晰度为: " << DR << endl;
    return true;
  }

  // else
  // cout<<"画面清晰，清晰度为: "<<DR<<endl;
  return false;
}